﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Newtonsoft.Json;
using NB.MGJ.Common.Utility;

namespace NB.MGJ.Common.API
{
    /// <summary>
    /// API控制器
    /// </summary>
    public class ApiBaseController<T> : ApiController where T : class
    {
        public T LogOnUser { get; set; }

        /// <summary>
        /// 返回业务成功的对象结构
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public IHttpActionResult ApiSuccess(ModelStateDictionary modelState)
        {
            var _message = "";
            var keys = modelState.Keys;
            var diction = new Dictionary<string, string>();
            foreach (var item in keys)
            {
                if (!string.IsNullOrWhiteSpace(_message)) continue;
                if (modelState[item].Errors.Count == 0) continue;
                if (!string.IsNullOrEmpty(item))
                {
                    _message = modelState[item].Errors[0].ErrorMessage;
                    //diction.Add(item, modelState[item].Errors[0].ErrorMessage);
                }
            }
            return this.ApiFail(_message);
        }

        /// <summary>
        /// 返回业务成功的对象结构
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public IHttpActionResult ApiSuccess(object model)
        {
            return new ApiResult(new ApiBaseModel { success = true, message = "", result = model }, Request);
        }

        /// <summary>
        /// 返回业务失败的对象结构
        /// </summary>
        /// <param name="messageCode"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public IHttpActionResult ApiFail(string message, params object[] parameters)
        {
            return new ApiResult(
                new ApiBaseModel { success = false, message = string.Format(message, parameters) },
                Request,
                System.Net.HttpStatusCode.BadRequest);
        }

        public void CheckParameter(params string[] parameters)
        {
            if (parameters != null)
            {
                foreach (var item in parameters)
                {
                    if (string.IsNullOrWhiteSpace(item))
                        throw new ApiErrorException("参数缺失");
                }
            }
        }

        /// <summary>
        /// 返回JS字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public IHttpActionResult ApiJS(string key, object model)
        {

            var _result = string.Format("var {0} = {1}", key, JsonConvert.SerializeObject(model));

            return new ApiResult(_result, Request);
        }

        public IHttpActionResult ApiJS(Dictionary<string, object> dic)
        {

            StringBuilder _builder = new StringBuilder();

            foreach (var item in dic)
            {
                _builder.AppendFormat("var {0} = {1} ;", item.Key, JsonConvert.SerializeObject(item.Value));
            }

            return new ApiResult(_builder.ToString(), Request);
        }


        /// <summary>
        ///用登录用户IP 
        /// </summary>
        public string UserIp { get; set; }
    }
}
